package com.ziyia.a3_2_all;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    // 定义飞机的移动速度
    private int speed = 10;
    private PlaneView planeView;
    DisplayMetrics metrics;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 创建PlaneView组件
        planeView = new PlaneView(this);
        setContentView(planeView);
        planeView.setBackgroundResource(R.drawable.background);
        // 获取窗口管理器
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        metrics = new DisplayMetrics();
        // 获得屏幕宽和高
        display.getMetrics(metrics);
        // 设置飞机的初始位置
        planeView.currentX = (metrics.widthPixels / 2);
        planeView.currentY = (metrics.heightPixels - 700);
        planeView.setOnTouchListener(new MyTouchListener());
    }
    class MyTouchListener implements View.OnTouchListener
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getX() < metrics.widthPixels / 8) {
                planeView.currentX -= speed;
            }
            if (event.getX() > metrics.widthPixels * 7 / 8) {
                planeView.currentX += speed;
            }
            if (event.getY() < metrics.heightPixels / 8) {
                planeView.currentY -= speed;
            }
            if (event.getY() > metrics.heightPixels * 7 / 8) {
                planeView.currentY += speed;
            }
            return true;
        }
    }
//    private int speed = 10;
//    private MyPlaneView planeView;
//    DisplayMetrics metrics;
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//        planeView = new MyPlaneView(this);
//        setContentView(planeView);
//        planeView.setBackgroundColor(Color.RED);
//
//
//        WindowManager windowManager = getWindowManager();
//
//        Display display = windowManager.getDefaultDisplay();
//
//        metrics = new DisplayMetrics();
//
//        display.getMetrics(metrics);
//
//        planeView.currentX = (metrics.widthPixels / 4);
//        planeView.currentY = (metrics.heightPixels - 700);
//        planeView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getX() < metrics.widthPixels  / 8) {
//                    planeView.currentX -= speed;
//                }
//                if (event.getX() > metrics.widthPixels * 7 / 8) {
//                    planeView.currentX += speed;
//                }
//
//                if (event.getY() < metrics.heightPixels  / 8) {
//                    planeView.currentY -= speed;
//                }
//                if (event.getY() > metrics.heightPixels* 7 / 8) {
//                    planeView.currentY += speed;
//                }
//
//                return true;
//            }
//        });
//    }
}

class PlaneView extends View
{
    float currentX;
    float currentY;
    // 创建画笔
    private Paint p = new Paint();
    private Bitmap plane0;
    private Bitmap plane1;
    private int index;
    public PlaneView(Context context)
    {
        super(context);
        // 定义飞机图片
        plane0 = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane0);
        plane1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane1);
        // 启动定时器来切换飞机图片，实现动画效果
        new Timer().schedule(new TimerTask()
        {
            @Override public void run()
            {
                index++;
                PlaneView.this.invalidate();
            }
        }, 0L, 100L);
        setFocusable(true);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        // 绘制飞机
        canvas.drawBitmap(index % 2 == 0 ? plane0 : plane1, currentX, currentY, p);
    }
}

